package com.bjpowernode.factory;

import java.lang.reflect.Method;

import com.bjpowernode.service.SomeService;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class MyCglibFactory implements MethodInterceptor {
	private SomeService target;

	public MyCglibFactory() {
		target = new SomeService();
	}

	public SomeService myCglibCreator() {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(SomeService.class);
		enhancer.setCallback(this);
		return (SomeService) enhancer.create();
	}

	@Override
	public Object intercept(Object arg0, Method method, Object[] args, MethodProxy arg3) throws Throwable {
		Object result = method.invoke(target, args);
		if (result != null) {
			return result.toString().toUpperCase();
		}
		return result;
	}
}
